package com.hspedu.draw;

import javax.swing.*;
import java.awt.*;

public class DrawCircle extends JFrame{

    private MyPanel mp = null;
    public static void main(String[] args) {
        new DrawCircle();
    }

    public DrawCircle() { //构造器
        mp = new MyPanel(); //初始化面板
        this.add(mp); //把面板放入到窗口
        this.setSize(500, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点击窗口x程序完全退出
        this.setVisible(true); //可以显示
    }
}

class MyPanel extends JPanel {
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画圆
        g.drawOval(100, 100, 200, 200);

        //画直线
        g.drawLine(10, 10, 100, 100);

        //画矩形边框
        g.drawRect(10, 10, 100, 100);

        //画填充矩形
        g.setColor(Color.blue); //设置画笔颜色
        g.fillRect(10, 10, 100, 100);

        //填充椭圆
        g.setColor(Color.red);
        g.fillOval(10,100,100,100);

        //图片
        //1.获取图片资源,从out/chapter15为根目录
        Image image = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/png2025-07-13-215200.png"));
        g.drawImage(image, 10, 10, 427, 289,this);

        //写字
        g.setColor(Color.red);
        g.setFont(new Font("黑体", Font.BOLD, 50));
        g.drawString("北京你好", 100, 100);
    }

}
